package com.ruoyi.web.controller.question;

import cn.hutool.core.util.EnumUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.question.domain.QQuestion;
import com.ruoyi.question.enums.QuestionTypeEnum;
import com.ruoyi.question.service.IQQuestionService;
import com.ruoyi.web.controller.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 题目Controller
 *
 * @author zouhao
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/system/question")
public class QQuestionController extends BaseController<QQuestion>
{
    @Autowired
    private IQQuestionService qQuestionService;
    @Override
    protected IService<QQuestion> getService() {
        return qQuestionService;
    }
    /**
     * 查询题目列表
     */
//    @PreAuthorize("@ss.hasPermi('system:question:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(Integer pageNum,Integer pageSize,QQuestion qQuestion)
//    {
//        return super.list(pageNum,pageSize,qQuestion);
//    }

    @PreAuthorize("@ss.hasPermi('system:question:list')")
    @GetMapping("/list")
    public AjaxResult list(QQuestion question)
    {
        List<QQuestion> list=qQuestionService.lambdaQuery()
                .like(StringUtils.isNotEmpty(question.getQuestionName()),QQuestion::getQuestionName,question.getQuestionName())
                .orderByAsc(QQuestion::getSort,QQuestion::getId)
                .list();
        return AjaxResult.success(list);
    }

    @PutMapping("/saveSoft")
    public AjaxResult saveSoft(@RequestBody List<QQuestion> list){
        return AjaxResult.success(qQuestionService.updateBatchById(list));
    }

    /**
     * 导出题目列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:export')")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QQuestion qQuestion)
    {
        super.exportExcel(response,qQuestion,"题目数据");
    }

    @RequestMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws IOException {
        qQuestionService.importData(file.getInputStream());
        return success("导入成功");
    }

    /**
     * 获取题目详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return super.getInfo(id);
    }

    /**
     * 新增题目
     */
    @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QQuestion qQuestion)
    {
        return super.add(qQuestion);
    }

    /**
     * 修改题目
     */
    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QQuestion qQuestion)
    {
        return super.edit(qQuestion);
    }

    /**
     * 删除题目
     */
    @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return super.remove(ids);
    }

    @GetMapping("/typeList")
    public AjaxResult typeList(){
        Map<String, QuestionTypeEnum> enumMap = EnumUtil.getEnumMap(QuestionTypeEnum.class);
        return AjaxResult.success(enumMap);
    }
}
